package tcl.loan.system.web;


import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import tcl.loan.system.common.config.Constants;
import tcl.loan.system.common.config.Global;
import tcl.loan.system.utls.UserUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by linjianding on 2016/12/5.
 */
@Controller

public class AccountController extends ControllerBase {
      private static final String Account_LoginView= "login";
    /**
     * 用户登陆
     */
    @RequestMapping("/login")
    public String login(Model model, HttpServletRequest request,HttpSession session)
    {
        if (UserUtils.getCurrentUserName() != null) {
            return "redirect:" + Global.getHomePath();
        }
        if (request.getParameter(Constants.LOGIN_ERROR_KEY) != null) {
            model.addAttribute(Constants.LOGIN_ERROR_KEY, request.getParameter(Constants.LOGIN_ERROR_KEY));
        }
        java.util.Random r = new java.util.Random();
        int rNum = r.nextInt(4) + 1;
        session.setAttribute("loginViewName", Account_LoginView);
        return Account_LoginView;
    }

    @RequestMapping(method = RequestMethod.POST)
    public String fail(@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) String userName, Model model, HttpSession session) {
        model.addAttribute(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM, userName);
        String loginViewName = session.getAttribute("loginViewName").toString();
        return loginViewName;
    }
}
